package cn.tedu.coolsharkmall.controller;

import cn.tedu.coolsharkmall.pojo.dto.AlbumAddNewDTO;
import cn.tedu.coolsharkmall.pojo.dto.AlbumUpdateDTO;
import cn.tedu.coolsharkmall.pojo.vo.AlbumListItemVo;
import cn.tedu.coolsharkmall.pojo.vo.AlbumStandardVo;
import cn.tedu.coolsharkmall.service.IAlbumService;
import cn.tedu.coolsharkmall.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Slf4j
@RestController
@RequestMapping("/albums")
@Api(tags = "01. 相册管理模块")
@Validated
public class AlbumController {
    @Autowired
    IAlbumService service;

    @PostMapping("/add-new")
    @ApiOperation("添加相册")
    @ApiOperationSupport(order = 100)
    public JsonResult<Void> addNew(@Validated AlbumAddNewDTO albumAddNewDTO) {
        log.debug("albumAddNewDTO" + albumAddNewDTO);
        service.addNew(albumAddNewDTO);
        return JsonResult.ok();
    }

    /*
    localhost:8080//album/delete?id=?
     */
    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperation("删除相册")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "相册ID", dataType = "long", required = true, example = "1"),
    })
    @ApiOperationSupport(order = 200)
    public JsonResult<Void> delete(@PathVariable @Range(min = 1, message = " 请提交有效的id值!") Long id) {
        service.delete(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/update")
    @ApiOperation("修改相册")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "相册ID", dataType = "long", required = true, example = "1"),
    })
    @ApiOperationSupport(order = 300)
    public JsonResult<Void> updateInfoById(@PathVariable @Range(min = 1, message = " 请提交有效的id值!") Long id,
                                     @Validated AlbumUpdateDTO albumUpdateDTO) {
        service.updateInfoById(id, albumUpdateDTO);
        return JsonResult.ok();
    }

    @GetMapping("")
    @ApiOperation("查询相册列表")
    @ApiOperationSupport(order = 420)
    public JsonResult<List<AlbumListItemVo>> list() {
        List<AlbumListItemVo> list = service.list();
        return JsonResult.ok(list);
    }

    @GetMapping("/{id:[0-9]+}")
    @ApiOperation("查询相册")
    @ApiOperationSupport(order = 430)
    public JsonResult<AlbumStandardVo> getAlbumById(@PathVariable @Range(min = 1, message = " 请提交有效的id值!") Long id){
        AlbumStandardVo queryResult = service.getStandardById(id);
        return JsonResult.ok(queryResult);
    }


}
